package com.garen.projects.demo.consumer.handler;

import com.garen.projects.demo.provider.model.Response;
import com.garen.projects.demo.provider.model.user.UserQuery;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/consumer/user")
public class UserController {

    public static final String USER_QUERY_URL = "http://DEMO-PROVIDER/user/{id}";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    public Response<UserQuery> query(@PathVariable("id") Long id) {
        Map<String, Long> params = ImmutableMap.<String, Long>builder().put("id", id).build();
        return restTemplate.getForObject(USER_QUERY_URL, Response.class, params);
    }
}
